@extends('admin.layouts.layout')
@section('content')
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox-title">
                <h5>添加角色</h5>
            </div>
            <div class="ibox-content">
                {{--                <a class="menuid btn btn-primary btn-sm" href="javascript:history.go(-1)">返回</a>--}}
                <a href="{{getBackUrl()}}">
                    <button class="btn btn-primary btn-sm" type="button"><i class="fa fa-plus-circle"></i> 返回</button>
                </a>
                <div class="hr-line-dashed m-t-sm m-b-sm"></div>
                <form class="form-horizontal m-t-md" action="{{route('roles.update',$role->id)}}" method="post">
                    {!! csrf_field() !!}
                    {{method_field('PATCH')}}
                    <input type="hidden" name="goBackUrl" value="{{getBackUrl()}}"/>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">角色名称：</label>
                        <div class="input-group col-sm-2">
                            <input type="text" class="form-control" name="name" value="{{$role->name}}" required
                                   data-msg-required="请输入角色名称">
                            @if ($errors->has('name'))
                                <span class="help-block m-b-none"><i class="fa fa-info-circle"></i>{{$errors->first('name')}}</span>
                            @endif
                        </div>
                    </div>
                    <div class="hr-line-dashed m-t-sm m-b-sm"></div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">角色描述：</label>
                        <div class="input-group col-sm-3">
                            <textarea name="remark" class="form-control" rows="5" cols="20"
                                      data-msg-required="请输入角色描述">{{$role->remark}}</textarea>
                            @if ($errors->has('remark'))
                                <span class="help-block m-b-none"><i class="fa fa-info-circle"></i>{{$errors->first('remark')}}</span>
                            @endif
                        </div>
                    </div>
                    <div class="hr-line-dashed m-t-sm m-b-sm"></div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">排序：</label>
                        <div class="input-group col-sm-1">
                            <input type="text" class="form-control" name="order" value="{{$role->order}}" required
                                   data-msg-required="请输入排序">
                            @if ($errors->has('order'))
                                <span class="help-block m-b-none"><i class="fa fa-info-circle"></i>{{$errors->first('order')}}</span>
                            @endif
                        </div>
                    </div>
                    <div class="hr-line-dashed m-t-sm m-b-sm"></div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">状态：</label>
                        <div class="input-group col-sm-1">
                            <select class="form-control" name="status">
                                <option value="1" @if($role->status == 1) selected="selected" @endif>启用</option>
                                <option value="2" @if($role->status == 2) selected="selected" @endif>禁用</option>
                            </select>
                            @if ($errors->has('status'))
                                <span class="help-block m-b-none"><i class="fa fa-info-circle"></i>{{$errors->first('status')}}</span>
                            @endif
                        </div>
                    </div>
                    <div class="hr-line-dashed m-t-sm m-b-sm"></div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">游戏：</label>
                        <div class="input-group col-sm-9">
                            <table class="table table-striped table-bordered table-hover table-condensed"
                                   style="margin: 0px;">
                                <tbody>
                                <tr class="b-group">
                                    <td id="tdRoleCreateGameIds">
                                        <?php
                                        $html = '';
                                        $roleGameIds = $role->games()->get()->toArray();
                                        $roleGameIds = array_column($roleGameIds, 'game_id');
                                        foreach ($games as $game) {
                                            $game_name = $game["game_name"];
                                            $game_id = $game['game_id'];
                                            $checked = '';
                                            if (in_array($game_id, $roleGameIds)) {
                                                $checked = 'checked="checked"';
                                            }
                                            $html .= <<<EOF
                                                <div class="col-sm-1" style="min-width: 20rem"><label>&emsp;$game_name
                                                        <input type="checkbox" class="pull-left"
                                                               $checked
                                                               name="game_id[]"
                                                               value="$game_id">
                                                </label></div>
EOF;
                                        }
                                        echo $html;
                                        ?>
                                    </td>
                                </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="hr-line-dashed m-t-sm m-b-sm"></div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">游戏渠道：</label>
                        <div class="input-group col-sm-9">
                            <table class="table table-striped table-bordered table-hover table-condensed"
                                   style="margin: 0px;">
                                <tbody>
                                <tr class="b-group">
                                    <td id="tdChannels">
                                        <?php
                                        $html = '';
                                        $roleChannelRelationIds = $role->channels()->get()->toArray();
                                        $roleChannelRelationIds = array_column($roleChannelRelationIds, 'id');
                                        $roleGameIds = $role->games()->get()->toArray();
                                        $roleGameIds = array_column($roleGameIds, 'game_id');

                                        foreach ($channelIds as $channel) {
                                            $channel_name = "{$channel['game']['game_name']}({$channel['channel_name']})";
                                            $game_channel_id = $channel['id'];
                                            $game_id = $channel['game_id'];
                                            $show = 'display:none;';
                                            $checked = '';
                                            if (in_array($game_channel_id, $roleChannelRelationIds)) {
                                                $checked = 'checked="checked"';
                                            }
                                            if (in_array($game_id, $roleGameIds)) {
                                                $show = 'display:inline-block;';
                                            }
                                            $html .= <<<EOF
                                                <div class="col-sm-1" style="min-width: 20rem;$show" data-game-id="$game_id"><label>&emsp;$channel_name
                                                        <input type="checkbox" class="pull-left"
                                                               $checked
                                                               name="channel_id[]"
                                                               value="$game_channel_id">
                                                </label></div>
EOF;
                                        }
                                        echo $html;
                                        ?>
                                    </td>
                                </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="hr-line-dashed m-t-sm m-b-sm"></div>
                    <div class="form-group">
                        <div class="col-sm-12 col-sm-offset-2">
                            <button class="btn btn-primary" type="submit"><i class="fa fa-check"></i>&nbsp;保 存</button>
                            　
                            <button class="btn btn-white" type="reset"><i class="fa fa-repeat"></i> 重 置</button>
                        </div>
                    </div>
                    <div class="clearfix"></div>
                </form>
            </div>
        </div>
    </div>
@endsection

@section('js')
    <script>
        //游戏checkbox
        $('#tdRoleCreateGameIds').find(':checkbox').change(function () {
            var game_id = $(this).val();
            if (this.checked) {
                $('#tdChannels div[data-game-id="' + game_id + '"]').show().find("input:checkbox").prop('checked', true);
            } else {
                $('#tdChannels div[data-game-id="' + game_id + '"]').hide().find("input:checkbox").prop('checked', false);
            }
        });
    </script>
@endsection